/***************************************************************************
 *   Antc - library                                                        *
 *                                                                         *
 *   Copyright (C) 2008 by kuerant                                         *
 *                         <kuerant@gmail.com>                             *
 *                                                                         *
 *   This library is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU Library General Public License as       *
 *   published by the Free Software Foundation; either version 2 of the    *
 *   License, or (at your option) any later version.                       *
 *                                                                         *
 *   This library is distributed in the hope that it will be usefull, but  *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      *
 *   Library General Public License for more details.                      *
 *                                                                         *
 *   You should have received a copy of the GNU Library General Public     *
 *   License along with this library; if not, write to the Free Software   *
 *   Foundation, Inc., 59 Temple Place - Suite 330, Boston,                *
 *   MA 02111-1307, USA                                                    *
 ***************************************************************************/


#ifndef ANTC_UDPCHANNEL_HPP
#define ANTC_UDPCHANNEL_HPP

#include    <stdio.h>
#include    <stdarg.h>

#include    <string>
#include    <deque>
#include    <list>

#include    "zsocket/udper.hpp"

#include    "common/exception.hpp"
#include    "common/packet.hpp"

namespace   antc
{

class   UdpChannel
{

public  :

    UdpChannel(std::string server_ip, int server_port);
    UdpChannel(zsocket::Udper& udper);
    ~UdpChannel();
    
public  :
    void    send(ant::Packet& packet, int timeout=-1) throw (ant::Exception);
    void    recv(ant::Packet& packet, int timeout=-1) throw (ant::Exception);

    void    send(zsocket::Address& address, ant::Packet& packet, int timeout=-1) throw (ant::Exception);
    void    recv(zsocket::Address& address, ant::Packet& packet, int timeout=-1) throw (ant::Exception);

private :

    void    init(void);
    void    trace(const char* pszFormat, ... );
    bool    traceable(void);
            
private :
    
    zsocket::Udper      m_udper;
    
    std::string         m_strServerAddress;
    int                 m_iServerPort;
    
    zsocket::Address    m_addressServer;

    struct  PacketIncomeCell
    {
        PacketIncomeCell(ant::Packet::serial_type serial, ant::Packet* pPacket, zsocket::Address* pAddress)
            : m_serial(serial), m_pPacketData(pPacket), m_pAddress(pAddress)
        {}
        
        ant::Packet::serial_type m_serial;
        ant::Packet*             m_pPacketData;
        zsocket::Address*   m_pAddress;
    };
    typedef std::list<PacketIncomeCell>    queue_income_packet_type;
    
    queue_income_packet_type  m_queue_data_received;

    //queue_type  m_queue_data_received_ready;
    //queue_type  m_queue_ack_received;
    //queue_type  m_queue_sending;
    //queue_type  m_queue_tosend;
    
    typedef std::deque< ant::Packet::serial_type >   queue_serial_type;
    queue_serial_type   m_serials_income;
    #define     MAX_QUEUE_SERIALS_INCOME_LENGTH     2048
    #define     MIN_QUEUE_SERIALS_INCOME_LENGTH     1024
    
    FILE*   m_pFileTrace;
};  // class    UdpChannel
    
}   // namespace antc

#endif  // ANTC_UDPCHANNEL_HPP
